//
//  M4LaunchViewController.swift
//  M4
//
//  Created by YuChuang on 2024/8/6.
//

import UIKit

class M4LaunchViewController: UIViewController {
    
//    var animaComplete: (() -> Void)?
    
    var inTime: Int = Int(Date().timeIntervalSince1970 * 1000) 

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = COLOR_00479D
        
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        view.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        imageView.image = UIImage(named: "star_logo")
        
        
        DispatchQueue.main.asyncAfter(deadline: .now()+2) {
            if M4UserDataManager.m4UserShare.M4ProtocolAgree {
                (UIApplication.shared.delegate as! AppDelegate).M4SetAdjust()
                if M4UserDataManager.m4UserShare.M4Token == "" {
                    AppDelegate.M4JumpLoginPage()
                }
                else {
                    let t = M4TabBarViewController()
                    AppDelegate.keyWindow()?.rootViewController = t
//                    AppDelegate.keyWindow()?.insertSubview(t.view, at: 0)
                }
            }
            else {
                let vc = M4ProtocolViewController()
                let nav = M4UINavigationController(rootViewController: vc)
                AppDelegate.keyWindow()?.rootViewController = nav
//                AppDelegate.keyWindow()?.insertSubview(nav.view, at: 0)
            }
        }
        
        M4DeviceDataManager.m4DeviceShare.ips = UIDevice.getLocalIPAddresses() ?? []
        M4DeviceDataManager.m4DeviceShare.wifiName = UIDevice.getWifiName() ?? ""
        
        // 1.获取NSData类型
//        guard let filePath = Bundle.main.path(forResource: "M4Launch.gif", ofType: nil) else { return }
//        guard let fileData = NSData(contentsOfFile: filePath) else { return }
//                
//        // 2.根据Data获取CGImageSource对象
//        guard let imageSource = CGImageSourceCreateWithData(fileData, nil) else { return }
//                
//        // 3.获取gif图片中图片的个数
//        let frameCount = CGImageSourceGetCount(imageSource)
//        // 记录播放时间
//        var duration : TimeInterval = 0
//        var images = [UIImage]()
//        for i in 0..<frameCount {
//            // 3.1.获取图片
//            guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else { continue }
//            // 3.2.获取时长
//            guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) , let gifInfo = (properties as NSDictionary)[kCGImagePropertyGIFDictionary as String] as? NSDictionary,
//            let frameDuration = (gifInfo[kCGImagePropertyGIFDelayTime as String] as? NSNumber) else { continue }
//            duration += frameDuration.doubleValue
//            let image = UIImage(cgImage: cgImage)
//            images.append(image)
//            // 设置停止播放时现实的图片
//            if i == frameCount - 1 {
//                imageView.image = image
//            }
//        }
//        // 4.播放图片
//        imageView.animationImages = images
//        // 播放总时间
//        imageView.animationDuration = duration
//        // 播放次数, 0为无限循环
//        imageView.animationRepeatCount = 1
//        // 开始播放
//        imageView.startAnimating()
//        DispatchQueue.main.asyncAfter(deadline: .now()+duration) {
//            imageView.stopAnimating()
////            self.animaComplete?()
//            if M4UserDataManager.m4UserShare.M4ProtocolAgree {
//                if M4UserDataManager.m4UserShare.M4Token == "" {
//                    AppDelegate.M4JumpLoginPage()
//                }
//                else {
//                    AppDelegate.keyWindow()?.rootViewController = M4TabBarViewController()
//                }
//            }
//            else {
//                let vc = M4ProtocolViewController()
//                let nav = M4UINavigationController(rootViewController: vc)
//                AppDelegate.keyWindow()?.rootViewController = nav
//            }
//        }
        
    }
    
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        M4HiddenPointDataManager.M4LuanckUploadHiddenPoint(inTime: inTime)
    }

}
